package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.activity.ActivityDTO;
import com.qk.entity.Activity;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: Enid
 * @date: 2025/10/10 18:24
 * @description:
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public List<Activity> listOfType(Integer type) {
        return activityMapper.listOfType(type);
    }

    @Override
    public void delete(Integer id) {
        activityMapper.delete(id);
    }

    @Override
    public Activity getById(Integer id) {
        return activityMapper.getById(id);
    }

    @Override
    public void update(Activity activity) {
        boolean hasNullField = BeanUtil.hasNullField(activity, "id","discount", "voucher", "createTime", "updateTime");
        if(hasNullField){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public void addActivity(Activity activity) {
        //参数校验
        boolean hasNullField = BeanUtil.hasNullField(activity, "id","discount", "voucher", "createTime", "updateTime");
        if(hasNullField){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.addActivity(activity);
    }

    @Override
    public PageResult<Activity> list(ActivityDTO activityDTO) {
        PageHelper.startPage(activityDTO.getPage(),activityDTO.getPageSize());
        List<Activity> result = activityMapper.list(activityDTO);
        Page<Activity> page = (Page<Activity>) result;
        return PageResult.<Activity>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(result)
                .build();
    }

}
